<?php
require_once dirname(__FILE__) . '/wp-load.php';
require_once dirname(__FILE__) . '/simplehtmldom/simple_html_dom.php';

//初始运行 获得站点的地址
function get_goal_url_contains_url($goalurl)
{
    $html = file_get_html($goalurl);
    $urls = array();
    foreach ($html->find('a') as $element) {
        if ($element->href && $element->href != '/') {
            if (substr($element->href, 0, 4) != 'tel:' && substr($element->href, 0, 7) != 'mailto:' && substr($element->href, 0, 1) != '#' && substr($element->href, 0, 11) != 'javascript:') {
                if (!in_array($element->href, $urls) && count(explode($goalurl, $element->href)) > 1) {
                    $fenge_q = explode('?', $element->href);
                    if (count($fenge_q) > 1) {
                        $fen_ge_url = explode('=', $fenge_q[1]);
                        if ($fen_ge_url[0] == 'paged') {
                            $fen_ge_url[0] = 'page';
                        }
                        $element->href = rtrim($fenge_q[0], '/') . "/" . implode('/', $fen_ge_url) . "/";
                        if($fen_ge_url[1] == '1'){
                        	$element->href = rtrim($fenge_q[0], '/') . "/"; 
                        }
                    }
                    if (count(explode('#', $element->href)) > 1) {
                        if (!in_array(explode('#', $element->href)[0], $urls) && count(explode($goalurl, explode('#', $element->href)[0])) > 1) {
                            array_push($urls, explode('#', $element->href)[0]);
                        }
                    } else {
                        array_push($urls, $element->href);
                    }

                }
            }
        }
    }
    unset($html);
    foreach ($urls as $url) {
        $temphtml = file_get_html($url);
        foreach ($temphtml->find('a') as $temp) {
            if ($temp->href && $temp->href != '/') {
                if (substr($temp->href, 0, 4) != 'tel:' && substr($temp->href, 0, 7) != 'mailto:' && substr($temp->href, 0, 1) != '#' && substr($temp->href, 0, 11) != 'javascript:') {
                    if (!in_array($temp->href, $urls) && count(explode($goalurl, $temp->href)) > 1) {
                        $fenge_temp_q = explode('?', $temp->href);
                        if (count($fenge_temp_q) > 1) {
                            $fen_ge_temp_url = explode('=', $fenge_temp_q[1]);
                            if ($fen_ge_temp_url[0] == 'paged') {
                                $fen_ge_temp_url[0] = 'page';
                            }
                            $temp->href = rtrim($fenge_temp_q[0], '/s') . "/" . implode('/', $fen_ge_temp_url) . "/";
                            if($fenge_temp_q[1] == '1'){
	                       	$temp->href = rtrim($fenge_temp_q[0], '/') . "/" ;
	                    }
                        }
                        if (count(explode('#', $temp->href)) > 1) {
                            if (!in_array(explode('#', $temp->href)[0], $urls) && count(explode($goalurl, explode('#', $temp->href)[0])) > 1) {
                                array_push($urls, explode('#', $temp->href)[0]);
                            }
                        } else {
                            array_push($urls, $temp->href);
                        }
                    }
                }
            }
        }
        unset($temphtml);
    }
    return $urls;
}

//加入任务地址进入redis任务
function lpush_url_join_redis_key($urls, $key, $args)
{
    if (class_exists('Redis')) {
        $redis = new Redis();
        $redis->connect('127.0.0.1', 6379);

        if (is_array($urls)) {
            foreach ($urls as $url) {
                $json_data = json_encode(array('goalurl' => $url, 'source_lang' => $args['source_lang'], 'goal_lang' => $args['goal_lang']));
                $redis->lpush($key, $json_data);
            }
        }
        if (is_string($urls)) {
            $json_data = json_encode(array('goalurl' => $urls, 'source_lang' => $args['source_lang'], 'goal_lang' => $args['goal_lang']));
            $redis->lpush($key, $json_data);
        }
        return true;
    } else {
        return false;
    }
}

$more_url = get_goal_url_contains_url('http://demo50.huaqiutong.com');
$res = lpush_url_join_redis_key($more_url, 'translate_task_list', array('source_lang' => 'en', 'goal_lang' => 'zh'));
if ($res) {
    echo "操作成功";
} else {
    echo "操作失败";
}
echo "\n";
